package com.devsoft.foodfrenzy;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;
import org.andengine.util.adt.align.HorizontalAlign;
import org.andengine.util.adt.color.Color;

import com.devsoft.foodfrenzy.SceneManager.SceneType;

public class OptionsScene extends BaseScene implements IOnMenuItemClickListener{
	
	private HUD gameHUD;
	private Text musicText;
	private Text soundText;
	private Text controlsText;
	private Text skinsText;
	private MenuScene menuChildScene;	//menu scene is imported
	private final int MENU_LEFT = 0;
	private final int MENU_RIGHT = 1;
	private int skin_index = UserData.getInstance().getSkinPref();
	private int max_index = (UserData.getInstance().getCount() / 2);
	
	private void createMenuChildScene()
	{
	    menuChildScene = new MenuScene(camera);	//sets up the palette for the screen
	    menuChildScene.setPosition(400, 240);
	    
	    final IMenuItem LeftMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LEFT, resourcesManager.opt_left_region, vbom), 1.5f, 1);	//creates buttons
	    final IMenuItem RightMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_RIGHT, resourcesManager.opt_right_region, vbom), 1.5f, 1);
	    
	    menuChildScene.addMenuItem(LeftMenuItem);	//adds buttons to the menu
	    menuChildScene.addMenuItem(RightMenuItem);
	    
	    menuChildScene.buildAnimations();		//builds animation for button press; makes button bigger when pressed
	    menuChildScene.setBackgroundEnabled(false);
	    
	    LeftMenuItem.setPosition(skinsText.getX() - 740, skinsText.getY() - 250);	//sets button locations; need to resituate
	    RightMenuItem.setPosition(skinsText.getX() - 60, skinsText.getY() - 250);
	    
	    menuChildScene.setOnMenuItemClickListener(this);	//listen for press, act appropriately
	    
	    setChildScene(menuChildScene);
	    
	}
	
	private void createHUD()
    {
        gameHUD = new HUD();
        		
        // CREATE TEXT
        musicText = new Text(400, 340, resourcesManager.opt_font, "Music:ONOFF", new TextOptions(HorizontalAlign.LEFT), vbom){
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
                if (touchEvent.isActionUp())
                {
                	if((UserData.getInstance().getMusicPref()) == true){
                		musicText.setText("Music: ON");
                		musicText.setColor(Color.GREEN);
                		resourcesManager.main_menu_music.play();
                		UserData.getInstance().setMusicPref(false);
                	}
                	else if((UserData.getInstance().getMusicPref()) == false){
                		musicText.setText("Music: OFF");
                		musicText.setColor(Color.RED);
                		resourcesManager.main_menu_music.pause();
                		UserData.getInstance().setMusicPref(true);
                	}
                }
                return true;
            };
        };
        registerTouchArea(musicText);
        if((UserData.getInstance().getMusicPref()) == true){
        	musicText.setText("Music: OFF");
    		musicText.setColor(Color.RED);
    	}
    	else if((UserData.getInstance().getMusicPref()) == false){
    		musicText.setText("Music: ON");
    		musicText.setColor(Color.GREEN);
    	}
        
        soundText = new Text(400, 260, resourcesManager.opt_font, "SoundFX:ONOFF", new TextOptions(HorizontalAlign.LEFT), vbom){
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
                if (touchEvent.isActionUp())
                {
                	if((UserData.getInstance().getSoundPref()) == true){
                		soundText.setText("Sound FX: ON");
                		soundText.setColor(Color.GREEN);
                		UserData.getInstance().setSoundPref(false);
                	}
                	else if((UserData.getInstance().getSoundPref()) == false){
                		soundText.setText("Sound FX: OFF");
                		soundText.setColor(Color.RED);
                		UserData.getInstance().setSoundPref(true);
                	}
                }
                return true;
            };
        };
        registerTouchArea(soundText);
        if((UserData.getInstance().getSoundPref()) == true){
    		soundText.setText("Sound FX: OFF");
    		soundText.setColor(Color.RED);
    		
    	}
    	else if((UserData.getInstance().getSoundPref()) == false){
    		soundText.setText("Sound FX: ON");
    		soundText.setColor(Color.GREEN);
    	}
        
        controlsText = new Text(400, 180, resourcesManager.opt_font, "Controls:SwipeTouch", new TextOptions(HorizontalAlign.LEFT), vbom){
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
                if (touchEvent.isActionUp())
                {
                	if((UserData.getInstance().getControlsPref()) == true){
                		controlsText.setText("Controls: Touch");
                		UserData.getInstance().setControlsPref(false);
                	}
                	else if((UserData.getInstance().getControlsPref()) == false){
                		controlsText.setText("Controls: Swipe");
                		UserData.getInstance().setControlsPref(true);
                	}
                }
                return true;
            };
        };
        registerTouchArea(controlsText);
        controlsText.setColor(Color.BLACK);
        if((UserData.getInstance().getControlsPref()) == true){
    		controlsText.setText("Controls: Swipe");
    	}
    	else if((UserData.getInstance().getControlsPref()) == false){
    		controlsText.setText("Controls: Touch");
    	}
        
        skinsText = new Text(400, 100, resourcesManager.opt_font, "Skin:ChefTheDoctorMarioLuigiWarioWaLuigiMetJasonMasterChiefMegaManAstronautRaphaelFinnSuperSaiyanJangoFettBirthday", new TextOptions(HorizontalAlign.LEFT), vbom);
        skinsText.setColor(Color.BLACK);
        switch(UserData.getInstance().getSkinPref()){
		case 0:
			skinsText.setText("Skin: Chef");
			break;
		case 1:
			skinsText.setText("Skin: The Doctor");
    		break;
		case 2:
			skinsText.setText("Skin: Mario");
    		break;
		case 3:
			skinsText.setText("Skin: Luigi");
    		break;
		case 4:
			skinsText.setText("Skin: Wario");
    		break;
		case 5:
			skinsText.setText("Skin: WaLuigi");
    		break;
		case 6:
			skinsText.setText("Skin: Met");
    		break;
		case 7:
			skinsText.setText("Skin: Jason");
    		break;
		case 8:
			skinsText.setText("Skin: Master Chief");
    		break;
		case 9:
			skinsText.setText("Skin: MegaMan");
    		break;
		case 10:
			skinsText.setText("Skin: Astronaut");
    		break;
		case 11:
			skinsText.setText("Skin: Raphael");
    		break;
		case 12:
			skinsText.setText("Skin: Finn");
    		break;
		case 13:
			skinsText.setText("Skin: Super Saiyan");
    		break;
		case 14:
			skinsText.setText("Skin: Jango Fett");
    		break;
		case 15:
			skinsText.setText("Skin: Birthday");
    		break;
    	}
        
        gameHUD.attachChild(musicText);
        gameHUD.attachChild(soundText);
        gameHUD.attachChild(controlsText);
        gameHUD.attachChild(skinsText);
        
        camera.setHUD(gameHUD);
        
    }
	
	@Override
	public void createScene() 
	{
		createBackground();
	    createHUD();
	    createMenuChildScene();
	}
	
	private void createBackground()
	{
	    attachChild(new Sprite(400, 240, resourcesManager.options_background_region, vbom)
	    {
	        @Override
	        protected void preDraw(GLState pGLState, Camera pCamera) //makes picture quality smoother
	        {
	            super.preDraw(pGLState, pCamera);
	            pGLState.enableDither();
	            
	        }
	        
	    });

	}

	@Override
	public void onBackKeyPressed() 	//exit app when backed
	{
		SceneManager.getInstance().loadMenuScene(engine, this.getSceneType());
	}
	

	@Override
	public SceneType getSceneType()
	{
	    return SceneType.SCENE_OPTIONS;
	    
	}
	
	
	@Override
	public void disposeScene() 
	{
		camera.setHUD(null);
        camera.setCenter(400, 240);
        menuChildScene.detachSelf();
		menuChildScene.dispose();
        this.detachSelf();
        this.dispose();
	}
	
	private void changeSkin(int type)
	{
		if (type == 0)
		{
			if(skin_index > 0)
			{
				skin_index--;
			}
			else
			{
				skin_index = max_index;
			}
		}
		else if (type == 1)
		{
			if(skin_index < max_index)
			{
				skin_index++;
			}
			else
			{
				skin_index = 0;
			}
		}
		
    	switch(skin_index){
		case 0:
			skinsText.setText("Skin: Chef");
			break;
		case 1:
			skinsText.setText("Skin: The Doctor");
    		break;
		case 2:
			skinsText.setText("Skin: Mario");
    		break;
		case 3:
			skinsText.setText("Skin: Luigi");
    		break;
		case 4:
			skinsText.setText("Skin: Wario");
    		break;
		case 5:
			skinsText.setText("Skin: WaLuigi");
    		break;
		case 6:
			skinsText.setText("Skin: Met");
    		break;
		case 7:
			skinsText.setText("Skin: Jason");
    		break;
		case 8:
			skinsText.setText("Skin: Master Chief");
    		break;
		case 9:
			skinsText.setText("Skin: MegaMan");
    		break;
		case 10:
			skinsText.setText("Skin: Astronaut");
    		break;
		case 11:
			skinsText.setText("Skin: Raphael");
    		break;
		case 12:
			skinsText.setText("Skin: Finn");
    		break;
		case 13:
			skinsText.setText("Skin: Super Saiyan");
    		break;
		case 14:
			skinsText.setText("Skin: Jango Fett");
    		break;
		case 15:
			skinsText.setText("Skin: Birthday");
    		break;
    	}
    	
    	UserData.getInstance().setSkinPref(skin_index);
	}
	
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
	{
		switch(pMenuItem.getID())
	    {
			case MENU_LEFT:
				changeSkin(MENU_LEFT);
	            return true;
			case MENU_RIGHT:
				changeSkin(MENU_RIGHT);
				return true;
	        default:
	            return false;
	            
	    }
		
	}
	
}
